<!--
 * @Descripttion: 商城轮播图
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-28 15:36:07
-->
<template>
    <div>
        <top-nav></top-nav>
        <div class="page-main">
            <el-row class="page-top-operate">
                <lb-button icon='el-icon-plus' type='primary' @click="addEditMallsBanner('add')">添加{{$t('menu.Malls')}}轮播图</lb-button>
            </el-row>
            <el-table
            v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
            <el-table-column
              prop="company_text"
              label="公司名">
            </el-table-column>
            <el-table-column
              label="图片"
              width="180">
              <template slot-scope="scope">
                  <lb-image :src="scope.row.img" />
              </template>
            </el-table-column>
            <el-table-column
              prop="top"
              label="排序值">
            </el-table-column>
            <el-table-column
              label="是否启用">
                <template slot-scope="scope">
                    <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change="handleBannerUpdate(scope.row.id,scope.row.status)"
                    >
                  </el-switch>
                </template>
            </el-table-column>
            <el-table-column
              prop='create_time'
              label="创建时间">
              <template slot-scope="scope">
                  <p>{{scope.row.create_time | handleTime(1)}}</p>
                  <p>{{scope.row.create_time | handleTime(2)}}</p>
              </template>
            </el-table-column>
            <el-table-column
              prop="address"
              width="180px"
              label="操作">
                <template slot-scope="scope">
                  <div class="table-operate">
                    <lb-button size='mini' plain type='primary' @click="addEditMallsBanner('edit',scope.row.id)">{{$t('action.edit')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="confirmDel(scope.row.id)">{{$t('action.delete')}}</lb-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>
          <lb-page
          :batch='false'
          :page='page'
          :pageSize='limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
          </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      tableData: [],
      page: 1,
      limit: 10,
      total: 0
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let {page, limit} = this
      this.$api.getMallsBanners({page, limit}).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    handleBannerUpdate (id, status) {
      this.$api.editUpdateMallsBanner({id, status}).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t('tips.successOper'))
        }
      })
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.delMallsBanner({id}).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.page = this.page < Math.ceil((this.total - 1) / this.limit) ? this.page : Math.ceil((this.total - 1) / this.limit)
            this.getTableDataList()
          }
        })
      })
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTableDataList()
    },
    addEditMallsBanner (type, id) {
      let url = type === 'add' ? '/malls/editBanner' : `/malls/editBanner?id=${id}`
      this.$router.push(url)
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
